#include "mgr.h"

PluginMgr* PluginMgr::Instance()
{
    static PluginMgr instance;
    return &instance;
}

Plugin* PluginMgr::Resolve(std::string key)
{
    if (plugins_.find(key) == plugins_.end()) {
        return nullptr;
    }

    std::function<Plugin *()> creator = plugins_[key];
    return creator();
}

std::shared_ptr<Plugin> PluginMgr::ResolveShared(std::string key)
{
    Plugin *plugin = Resolve(key);
    return std::shared_ptr<Plugin>(plugin);
}

/*
template<class T>
void PluginMgr::Register(const char* name)
{
    std::string key(name);

    if (plugins_.find(key) != plugins_.end()) {
        std::string err = key + " has already exists.";
        throw std::invalid_argument(err.c_str());
    }

    std::function<Plugin *()> creator = []{ return new T(); };
    plugins_.emplace(key, creator);
}
*/
